package cn.hexcloud.dga.governance.assessor.calc;

import cn.hexcloud.dga.common.constant.MetaConst;
import cn.hexcloud.dga.ds.bean.TDsTaskDefinition;
import cn.hexcloud.dga.ds.bean.TDsTaskInstance;
import cn.hexcloud.dga.ds.service.TDsTaskInstanceService;
import cn.hexcloud.dga.governance.assessor.Assessor;
import cn.hexcloud.dga.governance.bean.AssessParam;
import cn.hexcloud.dga.governance.bean.GovernanceAssessDetail;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author jiangdan7
 */
@Component("TASK_FAILED")
public class TaskFailedAssessor extends Assessor {
    @Autowired
    TDsTaskInstanceService tDsTaskInstanceService;
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        if(assessParam.getTDsTaskDefinition() == null) {
            return;
        }
        QueryWrapper<TDsTaskInstance> failQueryWrapper = new QueryWrapper<TDsTaskInstance>().eq("name", assessParam.getTableMetaInfo().getSchemaName() + "." + assessParam.getTableMetaInfo().getTableName()).eq("date_format(start_time,'%Y-%m-%d')", assessParam.getAssessDate()).eq("state", MetaConst.DS_TASK_FAILED);
        List<TDsTaskInstance> failList = tDsTaskInstanceService.list(failQueryWrapper);
        if(failList.size()>0){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("存在失败任务");
        }
    }
}
